#!/usr/bin/env bash
deviceId=$1
host='http://agile.meiliworks.com/data_mgr/save/dashboard'
if [ $# != 1 ]; then
    echo "sh time.sh deviceId"
    exit  1;
fi

currentDir=$(cd "$(dirname "$0")"; pwd)

sh ${currentDir}/src/main/resources/apk-install-time.sh $deviceId

sh ${currentDir}/src/main/resources/apk-launcher-time.sh $deviceId

installTimeFile=${currentDir}/src/main/resources/install-time.txt
launcherTimeFile=${currentDir}/src/main/resources/launcher-time.txt

installTime=`cat ${installTimeFile}`
welcomeTime=`sed -n 1p ${launcherTimeFile}`
mainTime=`sed -n 2p ${launcherTimeFile}`


welcomeTime=`echo "scale=3; ${welcomeTime}/1000" | bc`
mainTime=`echo "scale=3; ${mainTime}/1000" | bc`

#如果小于1秒钟
startChar=`echo ${welcomeTime:0:1}`
echo $startChar
if [ "${startChar}" == "." ]; then
	welcomeTime="0"${welcomeTime}
fi

startChar=`echo ${mainTime:0:1}`
if [ "${startChar}" == "." ]; then
	mainTime="0"${mainTime}
fi

echo "install Time "${installTime}
echo "welcome "${welcomeTime}
echo "mainTime "${mainTime}

timestamp=`date "+%s"`

curl -X POST $host -d '{"env":"MOB", "type": "TIME", "runStartTime":"'${timestamp}'", "runEndTime":"'${timestamp}'", "data":{"install_time":'${installTime}',"welcome_time":'${welcomeTime}',"main_time":'${mainTime}'}}'
